//
//  VVSportTrackController.m
//  DingDingSport
//
//  Created by 王惠平 on 2017/4/23.
//  Copyright © 2017年 will wang. All rights reserved.
//

#import "VVSportTrackController.h"
#import "VVSportMapViewController.h"
#import "VVAnimator.h"
#import "VVSportMapModeController.h"

@interface VVSportTrackController ()<UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) VVSportMapViewController *mapVC;

@property (nonatomic, strong) VVAnimator *animator;

//距离
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

//时长
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation VVSportTrackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdateNote) name:@"VVSportLocationDidUpdateNote" object:nil];
    
    //通过代码设置字体
//    self.timeLabel.font = [UIFont fontWithName:@"DINCond-Bold" size:30];
}

//位置更新后响应
- (void)locationDidUpdateNote {
    
    //设置数据
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f",self.track.totalDistance];
    
    int time = (int)self.track.totalTime;
    
    self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", time / 3600,(time % 3600) / 60,time % 60];
    
    
}

- (instancetype)init {
    
    self = [super init];
    
    if (self) {
        
        //设置地图
        [self setupMapView];
        
        //设置转场样式
        self.modalPresentationStyle = UIModalPresentationCustom;
        
        //设置强引用
        self.animator = [VVAnimator new];
        
        //设置代理
        self.transitioningDelegate = self.animator;
        
    }
    
    return self;
}

- (void)setupMapView {
    
    
        VVSportMapViewController *mapVC = [[VVSportMapViewController alloc]init];
    
        mapVC.track = self.track;
    
        //1.使用父子控制器
        [self addChildViewController:mapVC];
    
        //2.添加内容视图为子控制器
        [self.view insertSubview:mapVC.view atIndex:0];
    
        //3.设置子视图的尺寸 添加子视图时一定要设置尺寸,避免出现autoresizing的错误
        mapVC.view.frame = self.view.bounds;
    
        //官方推荐
        [mapVC didMoveToParentViewController:self];
    
        self.mapVC = mapVC;
}


- (IBAction)clickCloseBtn:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)clickMapModeBtn:(id)sender {
    
    //Popover 弹出窗  属于modal的一种
    //创建地图类型控制器
    VVSportMapModeController *mapModeVC = [[VVSportMapModeController alloc]initWithSelectBlock:^(VVMapType mapType) {
      
        self.mapVC.mapType = mapType;
        
    }];
    
    //将当前地图类型传递给mapMode
    mapModeVC.mapType = self.mapVC.mapType;
    
    //modal展示样式  iOS会自动检测设备类型,如果是iPhone上使用popover,还是会使用默认的modal样式进行适配
    mapModeVC.modalPresentationStyle = UIModalPresentationPopover;
    
    //获取Popover对象 用于设置Popover相关的数据
    UIPopoverPresentationController *popover= mapModeVC.popoverPresentationController;
    
    //设置Popover的代理  取消Modal的设备适配
    popover.delegate = self;
    
    //设置Popover的大小 preferredContentSize只能用于修改进行Popover展示的控制器的视图的大小
    mapModeVC.preferredContentSize = CGSizeMake(300, 120);
    
    //设置箭头方向
    popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
    
    //设置来源视图/BarButtonItem  展示Popover之前,必须设置参考点(参考的控件)
    popover.sourceView = sender;
    
    //进行展示
    [self presentViewController:mapModeVC animated:YES completion:nil];
    
}

//注意取消Modal自适应需要实现UIPopoverPresentationControllerDelegate的基协议中的方法

//对Modal样式进行自适应时调用
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    
    //取消适配
    return UIModalPresentationNone;
    
}

- (void)setTrack:(VVSportTrack *)track {
    
    _track = track;
    
    //传递轨迹模型
    self.mapVC.track = track;
}

- (void)setTrackBtnCenter:(CGPoint)trackBtnCenter {
    
    _trackBtnCenter = trackBtnCenter;
    
    self.mapVC.trackBtnCenter = _trackBtnCenter;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
